package cn.bdqn.sptparent.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {


    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {

        registry.jsp("/WEB-INF/jsp/", ".jsp");
    }
    /**
     * 资源处理
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //映射static路径的请求到static目录下  解决swagger2和static放行冲突问题
        // 静态资源访问路径和存放路径配置
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        // swagger访问配置  localhost:8080/swagger-ui.html/#/
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/","classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 对所有路径应用CORS配置
                .allowedOrigins("http://localhost:8081") // 前端应用的URL
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD") // 允许的HTTP方法
                .allowedHeaders("*") // 允许前端携带的任何头部
                .exposedHeaders("Authorization") // 暴露给前端应用的头部
                .allowCredentials(true) // 是否允许发送Cookies
                .maxAge(3600); // 预检请求的缓存时间，单位秒
    }


}
